import requests
import base64

def create_file_in_git_repository(repo_url, file_path, markdown_content, commit_message, git_token, branch="main"):
    # 准备提交的数据
    data = {
        'message': commit_message,
        'content': base64.b64encode(markdown_content.encode('utf-8')).decode('utf-8'),
        'branch': branch,
        'committer': {
            'name': 'SJF-ECNU',
            'email': '10225501403@stu.ecnu.edu.cn'
        }
    }
    print(data)
    # 发起创建文件请求
    headers = {'Authorization': f'token {git_token}'}
    owner, repo_name = "SJF-ECNU", "DaSE-Introduction"  # 替换为实际的所有者和仓库名称
    response = requests.put(f"{repo_url}/api/v1/repos/{owner}/{repo_name}/contents/{file_path}", headers=headers, json=data)

    print(response.status_code)
    if response.status_code == 201:
        print(f"文件 '{file_path}' 成功创建并提交到 Git 仓库！")
    else:
        print(f"创建文件失败：{response.text}")

# 示例用法
repo_url = "https://github.com"  # 请注意：这里只需要基本的 GitHub URL，不需要具体的仓库路径
file_path = "file.md"
markdown_content = "# 新的 Markdown 内容\n\n这是一个示例。"
commit_message = "创建新的 Markdown 文件"
git_token = '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'

create_file_in_git_repository(repo_url, file_path, markdown_content, commit_message, git_token)
